package com.woniuxy.amusementparkos.controller.xxy;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.xxy.Store_Info;
import com.woniuxy.amusementparkos.service.xxy.StoreService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;

@Controller
@RequestMapping("/store/info")
public class StoreController {
	@Autowired
	StoreService storeService;
	
	//查询全部
	@RequestMapping("/selectAll")
	@ResponseBody
	public TableDataDto<Store_Info> selectAllStore(@RequestParam("page") int page,
			@RequestParam("limit") int pageSize) {
			page=(page-1)*pageSize;
		try {
			List<Store_Info> stores = storeService.selectAllStore( page, pageSize);
			long count = storeService.selectAllStoreCount();
			return	new TableDataDto<Store_Info>(0, "查询成功", count, stores);
		} catch (Exception e) {
			 e.printStackTrace();
			return	new TableDataDto<Store_Info>(500, e.getMessage(), null, null);
		}
	}
	//查询一个
	@RequestMapping("/selectOne")
	@ResponseBody
	public ResponseResult<Store_Info> selectOneStore(String store_id) {
		try {
			Store_Info store = storeService.selectStoreByStore_id(Integer.parseInt(store_id));
			return new ResponseResult<Store_Info>(0, "查询成功", store);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseResult<Store_Info>(500, e.getMessage(), null);
		}
	}
	@RequestMapping("/update_manager")
	@ResponseBody
	public ResponseResult<String>  updateStore_manager(Store_Info store_Info){
		try {
			storeService.updateStore_manager(store_Info);
			return new ResponseResult<String>(0, "修改成功",null );
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
		
	}
	@RequestMapping("/insert")
	@ResponseBody
	public ResponseResult<String>  insertStore(Store_Info store_Info){
		if(store_Info.getStore_name()==null||store_Info.getStore_name().equals("")) return new ResponseResult<String>(500, "商店名不能为空", null);
		if(store_Info.getStore_address()==null||store_Info.getStore_address().equals("")) return new ResponseResult<String>(500, "商店地址不能为空", null);
		if(store_Info.getStore_stats()==null) return new ResponseResult<String>(500, "商店状态不能为空", null);
		if(store_Info.getStore_type()==null||store_Info.getStore_type().equals("")) return new ResponseResult<String>(500, "商店类型不能为空", null);
		if(store_Info.getStore_type().equals("0")) store_Info.setStore_type("餐饮");
		if(store_Info.getStore_type().equals("1")) store_Info.setStore_type("零售");
		if(store_Info.getStore_type().equals("2")) store_Info.setStore_type("休闲");
		if(store_Info.getStore_type().equals("3")) store_Info.setStore_type("娱乐");
		if(store_Info.getStore_type().equals("4")) store_Info.setStore_type("服装");
		try {
			storeService.insertStore(store_Info);
			return new ResponseResult<String>(0, "增加成功",null );
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
		
	}
	@RequestMapping("/delete")
	@ResponseBody
	public ResponseResult<String>  deleteStore(String  store_id){
		try {
			storeService.deleteStore(Integer.parseInt(store_id));
			return new ResponseResult<String>(0, "删除成功", null);
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
	}
	@RequestMapping("/getKey")
	@ResponseBody
	public ResponseResult<String>  getKey(String  store_id){
		try {
			String key = storeService.getKey(Integer.parseInt(store_id));
			return new ResponseResult<String>(0, null, key);
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
	}
	//以下为路径跳转
	@RequestMapping("/storeInfo")
	public String  toStoreInfo(){
		return "/xxy/商家信息页";
	}
	
	
}
